var roleManager_page={};

roleManager_page.action = {
    initBody:function () {
        roleManager_page.interface.initTablePage();
        roleManager_page.action.bindEvent();
    },
    bindEvent:function () {
        $("#addBtn").click(function () {
            roleManager_page.interface.openFormPage();
            // roleManager_page.interface.initFormPage();
            return false;
        });
        $("#saveBtnMenu").click(function () {
            roleManager_page.action.saveMenuForm();
        });
        $("#cancelBtn").click(function () {
            roleManager_page.action.initBody();
            return false;
        });
    },

    //保存配置菜单权限
    saveMenuForm:function () {
        var dataId = $("#dataId").val();
        var nodesArray = roleManager_page.data.ztreeChecked.getCheckedNodes();
        var menuArray = new Array();
        for(var i=0;i<nodesArray.length;i++){
            menuArray.push(nodesArray[i].id);
        }
        var menuIds = JSON.stringify(menuArray);
        AppComm.ajax.post('/roleManager/ajax/saveMenuPer',{
            roleId:dataId,
            menuIds:menuIds
        });
    },

    deleteRole:function (roleId) {
        AppComm.ajax.post('/roleManager/ajax/deleteRole',{
            id:roleId
        },function (resultData) {
            roleManager_page.data.tableIns.reload();
        });
    },
    // 保存操作授权
    saveOperations:function (roleId, operationIds) {
        var operationD = new Array();
        for (var i=0; i<operationIds.length; i++) {
            operationD.push(operationIds[i].value);
        }
        var ids = operationD.join(",");
        AppComm.ajax.post('/roleManager/ajax/saveOpationPer',{
            roleId:roleId,
            ids:ids
        });
    }
};

roleManager_page.interface = {
    // 加载表格页
    initTablePage: function () {
        var html = template("roleManagerList_template");
        $(".roleManager_box").html(html);

        layui.use(['table', 'form', 'util'], function () {
            var table = layui.table;
            // var util = layui.util;
            // util.fixbar();

            //执行表格渲染
            var tableIns = table.render({
                height: 625,
                url: AppComm.url.getBaseURL() + '/roleManager/ajax/getRoleManagerList',
                elem: "#tb_list",
                page: true,
                limit: 14,
                loading: true,
                method: 'POST',
                contentType: 'application/json',
                id: 'id',
                cols: [[
                    // {
                    //     field: 'roleKey',
                    //     title: '角色关键字',
                    //     align:'left',
                    // },
                    {
                        field: 'id',
                        title: 'id',
                        width: 200,
                        align:'left',
                    },
                    {
                        field: 'roleName',
                        title: '角色名称',
                        width: 200,
                        align:'left',
                    },{
                        field:'orderId',
                        title:'排序号',
                        edit: 'text',
                        align:'left',
                        width:100
                    },{
                        field:'groupName',
                        title:'所属组织',
                        align:'left',
                        width:200
                    },{
                        field: 'comment',
                        title: '说明',
                        align:'left',
                        width:300
                    }, {
                        title: '操作',
                        fixed: 'right',
                        width: 280,
                        align: 'center',
                        toolbar: '#toolbar'
                    }
                ]]
            });

            roleManager_page.data.tableIns = tableIns;

            table.on('tool(tb_list)', function (obj) {
                var rowData = obj.data;
                var layEvent = obj.event;
                if (layEvent === 'editBtn') {
                    roleManager_page.interface.openFormPage(rowData.id);
                    // roleManager_page.interface.initFormPage(rowData.id);
                }
                if (layEvent === 'menuBtn') {
                    roleManager_page.interface.initMenuPage(rowData.id);
                    // roleManager_page.action.bindEvent();
                }
                if (layEvent === 'operateBtn') {
                    roleManager_page.interface.initOperatePage(rowData.id);
                }
                if (layEvent === 'deleteBtn') {
                    // if(rowData.roleKey=="admin"){
                    //     layer.msg("系统管理员角色不能删除！！",{icon: 2});
                    // }else{
                    AppComm.layer.confirm('确定要进行删除吗？', function () {
                        roleManager_page.action.deleteRole(rowData.id);
                    });
                    // }
                }
            });
            table.on('edit(tb_list)', function(obj){
                AppComm.ajax.post('/roleManager/ajax/updateOrderId', {
                    id:obj.data.id,
                    orderId:obj.value,
                }, function () {
                    roleManager_page.action.initBody();
                });
            });
            // 查询
            var form = layui.form;
            form.render(null, 'searchForm');
            form.on('submit(searchBtn)', function (data) {
                var postData = {
                    roleName: data.field.nameInput
                };
                tableIns.reload({
                    page: {curr:1},
                    where: postData
                });
                return false;
            });
        });
    },
    openFormPage:function(rowId){
        AppComm.layer.open('/manager/form/roleManagerForm', null, function (layero, index) {
            var iframeWin = AppComm.utils.iframeWin(layero);
            if (rowId != undefined && '' != rowId.trim()) {
                iframeWin.roleManagerForm_page.data.rowId = rowId;
            }
            iframeWin.roleManagerForm_page.action.initBody();
        });
        // var index = layer.open({
        //     type: 2,
        //     content: AppComm.url.getBaseURL()+'/manager/form/roleManagerForm',
        //     area: ['720px', '430px'],
        //     scrollbar: false,
        //     maxmin: true,
        //     success: function (layero, index) {
        //         var iframeWin = AppComm.utils.iframeWin(layero);
        //         if (rowId != undefined && '' != rowId.trim()) {
        //             iframeWin.roleManagerForm_page.data.rowId = rowId;
        //         }
        //         iframeWin.roleManagerForm_page.action.initBody();
        //     }
        // });

        // 获取特定iframe层的索引
        // var body = layer.getChildFrame('body', index);
        // 获取iframe页的DOM
        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
        // var iframeWin = window[layero.find('iframe')[0]['name']];
        // 给子页面传值
        // iframeWin.roleManagerForm_page.data.rowId = 2;
        // console.log(body.html()); //得到iframe页的body内容
        // body.find('input').val('Hi，我是从父页来的')
        // layer.full(index);
    },
    //初始化菜单授权页
    initMenuPage: function (dataId) {
        var ztreeLoad = layer.load();
        var params = {
            roleId: dataId,
        };

        var html = template("roleManagerForm_ztree", {});
        var roleAuthLayer = layer.open({
            type: 1,
            area: ['420px', '440px'], //宽高
            btn: ['提交', '取消'], //可以无限个按钮
            yes: function(index, layero){
                layer.close(roleAuthLayer);
                roleManager_page.action.saveMenuForm();
            },
            btnAlign: 'c',
            content: html
        });

        $("#dataId").val(dataId);
        var ztree = new ztreeUnit('treeDemo', '/roleManager/ajax/getMenuZtree', params);
        ztree.options.isExpandAll = true;
        ztree.setZtreeCheck(true);
        ztree.ztreeInit();
        roleManager_page.data.ztreeChecked = ztree;
        layer.close(ztreeLoad);
    },
    //选择组织界面初始化
    initGroupPage: function () {
        var html = template("roleManagerList_group", {});
        var groupLayer = layer.open({
            type: 1,
            // skin: 'layui-layer-rim', //加上边框
            area: '720px', //宽高
            offset: '50px',
            content: html
        });
        layui.use(['table', 'form'], function () {
            var table = layui.table;
            //执行表格渲染
            var groupTable = table.render({
                elem: "#group_list",
                url: AppComm.url.getBaseURL()+'/groupManager/ajax/getGroupList',
                page: {
                    layout: ['count', 'prev', 'page', 'next', 'skip']
                },
                limit: 10,
                heigth:"600px",
                loading: true,
                method: 'POST',
                contentType: 'application/json',
                id: 'group_list',
                cols: [[
                    {
                        field: 'groupName',
                        title: '组织名称',
                        width: 200,
                        align: 'left',
                        event: 'setSign',
                    },
                    {
                        field: 'parentName',
                        title: '上级组织',
                        width: 200,
                        align: 'left',
                        event: 'setSign',
                    },
                    {
                        field: 'comment',
                        title: '组织说明',
                        width: 200,
                        align: 'left',
                        event: 'setSign',
                    }
                ]]
            });

            table.on('tool(group_list)', function (obj) {
                var rowData = obj.data;
                roleManager_page.data.currGroupId = rowData.id;
                $("#groupName").val(rowData.groupName);
                $("#groupId").val(rowData.id);
                // var layEvent = obj.event;
                layer.close(groupLayer);

            });
            // 查询
            var form = layui.form;
            form.render(null, 'searchGroupForm');
            form.on('submit(searchGroup)', function (data) {
                var postData = {
                    groupName: data.field.groupNameInput
                };
                groupTable.reload({
                    page: {curr:1},
                    where: postData
                });
                return false;
            });
            return;
        });
    },
    //初始化操作授权页
    initOperatePage: function (dataId) {
        var html = template("roleManagerList_operation", {});

        var operateLayer = layer.open({
            type: 1,
            // skin: 'layui-layer-rim', //加上边框
            area: '480px', //宽高
            offset: '50px',
            content: html,
            btnAlign: 'c',
            btn: ['提交', '取消'], //可以无限个按钮
            yes: function(index, layero){
                layer.close(operateLayer);
                var getData = roleManager_page.data.transfer.getData("transferId");
                roleManager_page.action.saveOperations(dataId, getData);
            },
        });
        AppComm.ajax.post('/operationManager/ajax/getOperationByRoleIdTransfer4Role', {
            roleId:dataId
        }, function (resultData) {
            layui.use(['transfer', 'layer', 'util'], function(){
                var $ = layui.$
                    ,transfer = layui.transfer
                    ,layer = layui.layer
                    ,util = layui.util;
                roleManager_page.data.transfer = transfer;
                roleManager_page.data.transfer.render({
                    id:'transferId',
                    elem: '#transfer',
                    title: ['未授权操作', '已授权操作'],
                    showSearch: true,
                    data: resultData.data.allOperations,
                    value: resultData.data.haveOperations
                })

            });
        });
    }

};

roleManager_page.data = {
    'tableIns':undefined,
    ztreeChecked:null,
    operationTable:null,
    tableData:[],
    'currGroupId':''
};


AppComm.initFrame(roleManager_page);